package common;

import java.awt.Image;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;

public class Utilities {

	//Obtain the image URL
	public static Image createImage(String path, String description) {
	    URL imageURL = locateResource(path);
	    
	    if (imageURL == null) {
	        System.err.println("Resource not found: " + path);
	        return null;
	    } else {
	        return (new ImageIcon(imageURL, description)).getImage();
	    }
	}

	public static URL locateResource(String path) {
		URL imageURL = null;
		try {
			File file = new File(path);
			if(file.exists())
				imageURL = new File(path).toURI().toURL();
			else{
				ClassLoader cl = Utilities.class.getClassLoader();
				imageURL = cl.getResource(path);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return imageURL;
	}

}
